﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryInsuranceOrganisation : ADONetRepositoryBase<InsuranceOrganization>
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(InsuranceOrganization entity)
        {
            var cmd = new SqlCommand("InsuranceOrganizationInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("INN", entity.Inn));
            cmd.Parameters.Add(new SqlParameter("Name", entity.Name));
            cmd.Parameters.Add(new SqlParameter("AddressId", entity.Address.Id));
            logger.Trace("SqlCommand для записи данных Страховой организации в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("InsuranceOrganizationDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Страховой организации id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(InsuranceOrganization entity)
        {
            var cmd = new SqlCommand("InsuranceOrganizationUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("INN", entity.Inn));
            cmd.Parameters.Add(new SqlParameter("Name", entity.Name));
            cmd.Parameters.Add(new SqlParameter("AddressId", entity.Address.Id));
            logger.Trace("SqlCommand для обновления данных Страховой организации id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetInsuranceOrganization", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Страховой организации id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            var cmd = new SqlCommand("GetInsuranceOrganizationList", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            logger.Trace("SqlCommand для выборки всех существующих Страховых организаций сформирован");
            return cmd;
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetInsuranceOrganizationByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Страховой организации {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override InsuranceOrganization GetEntity(SqlDataReader sqlDataReader)
        {
            InsuranceOrganization org = new InsuranceOrganization();
            IRepository<Address> repAddr = new ADONetRepositoryAddress();
            org.Id = (int)sqlDataReader["Id"];
            org.Inn = sqlDataReader["INN"].ToString();
            org.Name = sqlDataReader["Name"].ToString();
            org.Address = repAddr.GetById(int.Parse(sqlDataReader.GetValue(4).ToString()));
            return org;
        }
    }
}
